// MyFamily - XML based genealogy program.
// Copyright (C) 2006  Doug Swisher
// See the file COPYING.txt for license details.

using System;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;


namespace DataModel.Tests
{
    [TestFixture]
    public class GedComImportBasics : ImportTests
    {

        private void CheckName(FamilyIndividual indi, string expectedGiven, string expectedSurname)
        {
            Assert.AreEqual(expectedGiven, indi.Name.GivenNames, "Given name mismatch, indi {0}", indi.Id);
            Assert.AreEqual(expectedSurname, indi.Name.Surname, "Surname mismatch, indi {0}", indi.Id);
        }


        private void CheckBirth(FamilyIndividual indi, string expectedDate, string expectedPlace)
        {
            foreach (FamilyEvent eve in indi.Events)
            {
                if (eve.Kind == "BIRT")
                {
                    Assert.AreEqual(expectedDate, eve.Date, "Birth date mismatch for indi {0}.", indi.Id);
                    Assert.AreEqual(expectedPlace, eve.Place, "Birth place mismatch for indi {0}.", indi.Id);
                    return;
                }
            }

            Assert.Fail("No birth event found for indi {0}", indi.Id);
        }


        private void CheckDeath(FamilyIndividual indi, string expectedDate, string expectedPlace)
        {
            foreach (FamilyEvent eve in indi.Events)
            {
                if (eve.Kind == "DEAT")
                {
                    Assert.AreEqual(expectedDate, eve.Date, "Death date mismatch for indi {0}.", indi.Id);
                    Assert.AreEqual(expectedPlace, eve.Place, "Death place mismatch for indi {0}.", indi.Id);
                    return;
                }
            }

            Assert.Fail("No death event found for indi {0}", indi.Id);
        }


        [Test]
        public void Blank()
        {
            FamilyInfo family = ReadGedCom("Blank.ged", 2);

            Assert.AreEqual(0, family.Individuals.Count, "Wrong number of individuals.");
        }


        [Test]
        public void GedComSpecSample()
        {
            FamilyInfo family = ReadGedCom("GedComSpecSample.ged", 0);

            Assert.AreEqual(3, family.Individuals.Count, "Wrong number of individuals.");

            int seen = 0;
            foreach (FamilyIndividual indi in family.Individuals.Values)
            {
                switch (indi.Id)
                {
                    case "1":
                        seen++;
                        this.CheckName(indi, "Robert Eugene", "Williams");
                        Assert.AreEqual(4, indi.Events.Count, "Indi {0} should contain two events.", indi.Id);
                        this.CheckBirth(indi, "02 OCT 1822", "Weston, Madison, Connecticut");
                        this.CheckDeath(indi, "14 APR 1905", "Stamford, Fairfield, CT");
                        break;

                    case "2":
                        seen++;
                        this.CheckName(indi, "Mary Ann", "Wilson");
                        this.CheckBirth(indi, "BEF 1828", "Connecticut");
                        break;

                    case "3":
                        this.CheckName(indi, "Joe", "Williams");
                        this.CheckBirth(indi, "11 JUN 1861", "Idaho Falls, Bonneville, Idaho");
                        seen++;
                        break;

                    default:
                        Assert.Fail("Unknown INDI id - {0} - found while enumerating individuals.", indi.Id);
                        break;
                }
            }

            Assert.AreEqual(3, seen, "Not all individuals were seen when enumerating.");
        }


        [Test]
        public void Minimal()
        {
            FamilyInfo family = ReadGedCom("Minimal.ged", 0);
            Assert.AreEqual(0, family.Individuals.Count, "Wrong number of individuals.");
        }


        [Test, Ignore]
        public void GedComTorture()
        {
            FamilyInfo family = ReadGedCom("Torture\\TGC55C.ged", 0);
        }


    }
}
